package view;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import action.StudentsAction;
import model.Students;

/**
 * MVC三层模型
 * 视图层
 * @author jinmingwu
 *
 */
public class View {

	//提示语
	private static final String CONTEXT = "=============================\n" + 
			"学生信息管理系统 \n" + 
			"功能列表： \n" + 
			"[MAIN/M]:显示主菜单\n" + 
			"[QUERY/Q]:查看所有学生信息\n" + 
			"[GET/G]:根据学生ID查看学生信息\n" + 
			"[ADD/A]:增加学生信息\n" + 
			"[UPDATE/U]:更新学生信息\n" + 
			"[DELETE/D]:删除学生信息\n" + 
			"[SEACHER/S]:查询学生信息（根据姓名、手机号来查询）\n" + 
			"[EXIT/E]:退出学生信息管理系统\n" + 
			"[BREAK/B]:退出当前功能，返回主菜单\n" + 
			" ";

	//操作标记
	private static final String OPERATION_MAIN = "MAIN";
	private static final String OPERATION_QUERY = "QUERY";
	private static final String OPERATION_GET = "GET";
	private static final String OPERATION_ADD = "ADD";
	private static final String OPERATION_UPDATE = "UPDATE";
	private static final String OPERATION_DELETE = "DELETE";
	private static final String OPERATION_SEACHER = "SEACHER";
	private static final String OPERATION_EXIT = "EXIT";
	private static final String OPERATION_BREAK = "BREAK";

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.out.println(CONTEXT);
		//接收控制条操作参数
		Scanner sc = new Scanner(System.in);
		
		Students student = new Students();
		StudentsAction action = new StudentsAction();
		List<Students> list = null;
		List<Map<String, Object>> params = new ArrayList<Map<String, Object>>();
		Map<String, Object> param = new HashMap<String, Object>();
		
		//记忆变量
		String prenious = null;
		//步骤标记
		Integer step = 1;
		
		//程序一直循环
		while(sc.hasNext()) {
			String in = sc.next().toString();
			
			if(OPERATION_EXIT.equals(in.toUpperCase()) || 
					OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())) {
				//退出系统
				System.out.println("您已经退出学生信息管理系统！再见！");
				break;
			} else if(OPERATION_BREAK.equals(in.toUpperCase()) || 
					OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())) {
				//退出当前功能，返回主菜单
				step = 1;
				prenious = null;
				System.out.println(CONTEXT);
			} else if(OPERATION_MAIN.equals(in.toUpperCase()) || 
					OPERATION_MAIN.substring(0, 1).equals(in.toUpperCase())) {
				//显示主菜单
				System.out.println(CONTEXT);
			} else if(OPERATION_QUERY.equals(in.toUpperCase()) || 
					OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase())) {
				//查看所有学生信息
				try {
					list = action.query();
					for (Students students : list) {
						System.out.println(students.toString());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else if(OPERATION_DELETE.equals(in.toUpperCase()) || 
					OPERATION_DELETE.substring(0, 1).equals(in.toUpperCase()) || 
					OPERATION_DELETE.equals(prenious)) {
				prenious = OPERATION_DELETE;
				//删除学生信息
				if(step == 1) {
					System.out.println("请输入需要删除学生的[ID]:");
				} else if(step == 2) {
					try {
						action.delete(Integer.valueOf(in));
						System.out.println("成功删除学生ID为：" + in + " 的信息！");
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.out.println("请输入符合的数字！");
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.out.println("ID不存在！\n 删除失败！");
					} finally {
						step = 1;
						prenious = null;
					}
				}
				if(OPERATION_DELETE.equals(prenious)) {
					++step;
				}
			} else if(OPERATION_GET.equals(in.toUpperCase()) || 
					OPERATION_GET.substring(0, 1).equals(in.toUpperCase()) || 
					OPERATION_GET.equals(prenious)) {
				prenious = OPERATION_GET;
				//根据学生ID查看学生信息
				if(step == 1) {
					System.out.println("请输入需要查询学生的[ID]:");
				} else if(step == 2) {
					try {
						student = action.get(Integer.valueOf(in));
						if(student == null) {
							System.out.println("ID不存在！");
						} else {
							System.out.println(student.toString());
						}
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.out.println("请输入符合的数字！");
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} finally {
						step = 1;
						prenious = null;
					}
				}
				if(OPERATION_GET.equals(prenious)) {
					++step;
				}
			} else if(OPERATION_UPDATE.equals(in.toUpperCase()) || 
					OPERATION_UPDATE.substring(0, 1).equals(in.toUpperCase()) || 
					OPERATION_UPDATE.equals(prenious)) {
				prenious = OPERATION_UPDATE;
				//更新学生信息
				if(step == 1) {
					System.out.println("请输入需要修改学生的[ID]:");
				} else if(step == 2) {
					student.setId(Integer.valueOf(in));
					System.out.println("请输入需要修改学生的[姓名]：");
				} else if(step == 3) {
					student.setName(in);
					System.out.println("请输入需要修改学生的[性别]：");
				} else if(step == 4) {
					student.setSex(in);
					System.out.println("请输入需要修改学生的[出生日期]：");
				} else if(step == 5) {
					student.setBirthday(in);
					System.out.println("请输入需要修改学生的[年龄]：");
				} else if(step == 6) {
					student.setAge(Integer.valueOf(in));
					System.out.println("请输入需要修改学生的[联系方式]：");
				} else if(step == 7) {
					student.setTel(in);
					try {
						action.update(student);
						System.out.println("修改学生信息成功！");
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println("修改学生信息失败！");
					} finally {
						step = 1;
						prenious = null;
					}
				}
				if(OPERATION_UPDATE.equals(prenious)) {
					++step;
				}
			} else if(OPERATION_ADD.equals(in.toUpperCase()) || 
					OPERATION_ADD.substring(0, 1).equals(in.toUpperCase()) || 
					OPERATION_ADD.equals(prenious)) {
				prenious = OPERATION_ADD;
				//新增学生
				if(step == 1) {
					System.out.println("请输入新增学生的[姓名]：");
				} else if(step == 2) {
					student.setName(in);
					System.out.println("请输入新增学生的[性别]：");
				} else if(step == 3) {
					student.setSex(in);
					System.out.println("请输入新增学生的[出生日期]：");
				} else if(step == 4) {
					student.setBirthday(in);
					System.out.println("请输入新增学生的[年龄]：");
				} else if(step == 5) {
					student.setAge(Integer.valueOf(in));
					System.out.println("请输入新增学生的[联系方式]：");
				} else if(step == 6) {
					student.setTel(in);
					try {
						action.add(student);
						System.out.println("新增学生信息成功！");
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println("新增学生信息失败！");
					} finally {
						step = 1;
						prenious = null;
					}
				}
				if(OPERATION_ADD.equals(prenious)) {
					++step;
				}
			} else if(OPERATION_SEACHER.equals(in.toUpperCase()) || 
					OPERATION_SEACHER.substring(0, 1).equals(in.toUpperCase()) || 
					OPERATION_SEACHER.equals(prenious)) {
				prenious = OPERATION_SEACHER;
				//查询学生信息（根据姓名、手机号来查询）
				
				if(step == 1) {
					System.out.println("请输入查询学生的[姓名]：");
				} else if(step == 2) {
					String tmpName = "'%" + in + "%'";
					param.put("name", "name");
					param.put("rela", "LIKE");
					param.put("value", tmpName);
					params.add(param);
					System.out.println("请输入新增学生的[联系方式]：");
				} else if(step == 3) {
					String tmptel = "'%" + in + "%'";
					param.put("name", "tel");
					param.put("rela", "LIKE");
					param.put("value", tmptel);
					params.add(param);
					
					try {
						list = action.query(params);
						for(int i = 0; i < list.size(); ++i) {
							System.out.println(list.get(i).toString());
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.out.println("查询失败！");
					}
				} 
				if(OPERATION_SEACHER.equals(prenious)) {
					++step;
				}
			} else {
				System.out.println("非法输入！您输入的值为：" + in);
			}

		}
	}
}
